package com.hoshiicloud.user.controller.platform;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.user.entity.platform.PlatUsers;
import com.hoshiicloud.user.param.PlatUsersParam;
import com.hoshiicloud.user.param.platform.PlatUsersInsertParam;
import com.hoshiicloud.user.param.platform.PlatUsersUpdateParam;
import com.hoshiicloud.user.service.platform.PlatUsersService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 用户基本信息表 前端控制器
 * </p>
 *
 * @author yl
 * @since 2019-04-20
 */
@Controller
@RequestMapping("/platform/platUsers")
@Api(value = "租户平台工作人员", tags = {"租户平台工作人员"})
@Slf4j
@ApiSort(23)
public class
PlatUsersController {

    @Autowired
    private PlatUsersService platUsersService;

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 分页查询平台用户信息
     */
    @ApiOperation(value = "分页查询平台用户记录")
    @PostMapping("/getPage")
    public Response page(@RequestBody @Valid PlatUsersParam param) {
        try {
            QueryWrapper<PlatUsers> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda()
                    .eq(StringUtils.isNotBlank(param.getUserCode()),PlatUsers::getUserCode, param.getUserCode())
                    .eq(StringUtils.isNotBlank(param.getWxName()),PlatUsers::getWxName,param.getWxName())
                    .eq(param.getMemberId()!=null,PlatUsers::getMemberId,param.getMemberId().toString());
                    //.eq(param.getPlatformId()!=null,getu);
            IPage<PlatUsers> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            String key = param.toString()+ ",Page{pageNumber="+param.getPageNumber()+",pageSize="+param.getPageSize()+"}";
            return Response.data(platUsersService.getPage(page,queryWrapper,key));
        } catch (Exception e) {
            log.error("分页查询平台用户信息异常",e);
            return Response.failed("分页查询平台用户信息异常");
        }
    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 根据id查询平台用户信息
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询平台用户信息")
    public Response queryPlatUsersById(@PathVariable("id") Long id) {
        return Response.data(platUsersService.getById(id));
    }

    /**
     * @param departmentId
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 根据平台id查询
     */
    @GetMapping("/query/departmentId/{departmentId}")
    @ApiOperation(value = "查询平台用户信息")
    public Response queryPlatformByName(@PathVariable("departmentId") Long departmentId) {
        return Response.data(platUsersService.queryPlatformByName(departmentId));
    }

    /**
     * @param platUsersInsertParam
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增平台用户信息")
    public Response savePlatUsers(@RequestBody @Valid PlatUsersInsertParam platUsersInsertParam) {
        PlatUsers entity;
        try {
            entity = BeanUtils.covertType(platUsersInsertParam, PlatUsers.class);
        } catch (Exception e) {
            log.error("新增平台用户信息Bean转换异常",e);
            return Response.failed("新增平台用户信息Bean转换异常");
        }
        return Response.data(platUsersService.save(entity));

    }

    /**
     * @param platUsersUpdateParam
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 修改
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改平台用户信息")
    public Response updatePlatUsersById(@RequestBody @Valid PlatUsersUpdateParam platUsersUpdateParam) {
        PlatUsers entity;
        try {
            entity = BeanUtils.covertType(platUsersUpdateParam, PlatUsers.class);
        } catch (Exception e) {
            log.error("修改平台用户信息Bean转换异常",e);
            return Response.failed("修改平台用户信息Bean转换异常");
        }
        return Response.data(platUsersService.updateById(entity));

    }
}

